package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.exceptionhandler.BusinessException;
import com.example.demo.exceptionhandler.EmBusinessError;
import com.example.demo.model.SysAcl;
import com.example.demo.model.SysRole;
import com.example.demo.dao.SysRoleMapper;
import com.example.demo.pageinfo.DoPage;
import com.example.demo.requesthandler.UserSession;
import com.example.demo.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.util.CodeGeneratorUtil;
import com.example.demo.util.IpAddressUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Webgasd
 * @since 2021-04-26
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 获取全部角色列表
     *
     * @return
     */
    @Override
    @DoPage
    public List<SysRole> getRoleList(String remark) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(remark), "remark", remark)
                .orderByAsc("id");
        return sysRoleMapper.selectList(queryWrapper);
    }

    /**
     * 新增一个角色
     *
     * @param sysRole
     */
    @Override
    public int saveRole(SysRole sysRole) {
        if (checkExist(sysRole.getName(), sysRole.getId())) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "角色名称已经存在");
        }
        sysRole.setType(1);
        sysRole.setOperator(UserSession.getLoginName());
        sysRole.setOperateTime(new Date());
        sysRole.setOperateIp(IpAddressUtil.getRemoteIp());
        return sysRoleMapper.insert(sysRole);
    }

    /**
     * 修改角色信息
     *
     * @param sysRole
     */
    @Override
    public int updateRole(SysRole sysRole) {
        if (checkExist(sysRole.getName(), sysRole.getId())) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "角色名称已经存在");
        }
        SysRole before = sysRoleMapper.selectById(sysRole.getId());
        if (before == null) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "待更新的角色不存在");
        }
        SysRole after = new SysRole();
        BeanUtils.copyProperties(sysRole, after);
        after.setOperator(UserSession.getLoginName());
        after.setOperateTime(new Date());
        after.setOperateIp(IpAddressUtil.getRemoteIp());
        return sysRoleMapper.updateById(after);
    }

    /**
     * 根据主键id删除某个角色
     *
     * @param id
     */
    @Override
    public int deleteRoleById(Integer id) {
        return sysRoleMapper.deleteById(id);
    }

    /**
     * 代码块——用于检查DB中是否已经存在该记录
     *
     * @param name
     * @param id
     * @return
     */
    private boolean checkExist(String name, Integer id) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(id != null, "id", id)
                .eq(StringUtils.isNotBlank(name), "name", name);
        return sysRoleMapper.selectCount(queryWrapper) > 0;
    }
}
